import { BrowserWindow } from 'electron';
import { StreamEventHandlers } from '../core/stream-events';

export class ElectronStreamHandler implements StreamEventHandlers {
  private mainWindow: BrowserWindow;

  constructor(mainWindow: BrowserWindow) {
    this.mainWindow = mainWindow;
    console.log('🔧 ElectronStreamHandler initialized');
  }

  onStreamStart = (round: number) => {
    try {
      console.log('📡 ElectronStreamHandler: Stream started', { round });
      this.mainWindow.webContents.send('ai-stream-start', { round, timestamp: Date.now() });
    } catch (error) {
      console.error('❌ ElectronStreamHandler.onStreamStart error:', error);
    }
  }

  onContent = (content: string) => {
    try {
      this.mainWindow.webContents.send('ai-stream-content', { content });
    } catch (error) {
      console.error('❌ ElectronStreamHandler.onContent error:', error);
    }
  }

  onReasoning = (reasoning: string) => {
    try {
      this.mainWindow.webContents.send('ai-reasoning', { reasoning });
    } catch (error) {
      console.error('❌ ElectronStreamHandler.onReasoning error:', error);
    }
  }

  onReasoningEnd = () => {
    try {
      this.mainWindow.webContents.send('ai-reasoning-end', {});
    } catch (error) {
      console.error('❌ ElectronStreamHandler.onReasoningEnd error:', error);
    }
  }

  onStreamComplete = () => {
    try {
      console.log('✅ ElectronStreamHandler: Stream completed');
      this.mainWindow.webContents.send('ai-stream-complete', { timestamp: Date.now() });
    } catch (error) {
      console.error('❌ ElectronStreamHandler.onStreamComplete error:', error);
    }
  }

  onToolCallsDetected = (toolCalls: any[]) => {
    try {
      this.mainWindow.webContents.send('ai-tool-calls-detected', { toolCalls });
    } catch (error) {
      console.error('❌ ElectronStreamHandler.onToolCallsDetected error:', error);
    }
  }

  onToolExecutionStart = (toolName: string, args: any) => {
    try {
      this.mainWindow.webContents.send('ai-tool-execution-start', { toolName, args });
    } catch (error) {
      console.error('❌ ElectronStreamHandler.onToolExecutionStart error:', error);
    }
  }

  onToolExecutionComplete = (toolName: string, result: string) => {
    try {
      this.mainWindow.webContents.send('ai-tool-execution-complete', { toolName, result });
    } catch (error) {
      console.error('❌ ElectronStreamHandler.onToolExecutionComplete error:', error);
    }
  }

  onToolExecutionError = (toolName: string, error: string) => {
    try {
      this.mainWindow.webContents.send('ai-tool-execution-error', { toolName, error });
    } catch (error) {
      console.error('❌ ElectronStreamHandler.onToolExecutionError error:', error);
    }
  }

  onConversationComplete = () => {
    try {
      console.log('🎯 ElectronStreamHandler: Conversation completed');
      this.mainWindow.webContents.send('ai-conversation-complete', { timestamp: Date.now() });
    } catch (error) {
      console.error('❌ ElectronStreamHandler.onConversationComplete error:', error);
    }
  }

  onError = (error: Error) => {
    try {
      console.error('❌ ElectronStreamHandler: Error event received:', error);
      this.mainWindow.webContents.send('ai-error', { error: error.message });
    } catch (err) {
      console.error('❌ ElectronStreamHandler.onError internal error:', err);
    }
  }

  onDebug = (message: string, data?: any) => {
    try {
      console.log('🐛 ElectronStreamHandler Debug:', message, data);
      this.mainWindow.webContents.send('ai-debug', { message, data });
    } catch (error) {
      console.error('❌ ElectronStreamHandler.onDebug error:', error);
    }
  }
}